﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using PPI.Data.Utility;
using PPI.Entities;
using PPI.Entities.Interface;
using PPI.Entities.Lookup;
using PPI.Entities.Utility;
using PPI.Mobile;
using VMind.Global;

namespace PPI.ServiceLogic.Utilty
{
    public class LUtility_Asset_XMissionStructure  : IMobileTable
    {

        public static Int32 DuplicateByInspection(Int32 inspectionId, string connectionKey)
        {
            DUtility_Asset_XMissionStructure.DuplicateByInspection(inspectionId, connectionKey);
            return 1;

        }

        public static Int32 DuplicateByVisit(Int32 visitId, string connectionKey)
        {
            DUtility_Asset_XMissionStructure.DuplicateByVisit(visitId, connectionKey);
            return 1;
        }

        public static Int32 AssociateWithVisit(Int32 visitId,Int32 utilityId, string connectionKey)
        {
            DUtility_Asset_XMissionStructure.AssociateWithVisit(visitId, utilityId,connectionKey);
            return 1;
        }
        
        public static Int32 AssociateWithInspection(Int32 inspectionId, Int32 utilityId, string connectionKey)
        {
            DUtility_Asset_XMissionStructure.AssociateWithInspection(inspectionId, utilityId, connectionKey);
            return 1;
        }

        public static void AutolinkInspection(double maxdistanceInMts,string connectionKey)
        {
            DUtility_Asset_XMissionStructure.AutolinkInspection(maxdistanceInMts, connectionKey);
        }

        public static void AutolinkVendor(double maxdistanceInMts,string connectionKey)
        {
            DUtility_Asset_XMissionStructure.AutolinkVendor(maxdistanceInMts, connectionKey);
        }

        public List<EMobileQuery> CreateInsertMobileQuery(string connectionKey)
        {
            List<EUtility_Asset_XMissionStructure> listAssets = new List<EUtility_Asset_XMissionStructure>();
            DataSet dsAssets = DUtility_Asset_XMissionStructure.SelectAll(connectionKey);
            List<EMobileQuery> listQueries = new List<EMobileQuery>();
            if(dsAssets != null)
            {
                DataTable dtAssets = dsAssets.Tables[0];
                listAssets = ParseDatatableAsset(dtAssets);

                EMobileQuery _MobileAssets = new EMobileQuery();
                StringBuilder _QueryAssets = new StringBuilder();
                List<SqlParameter> _ParametersAssets = null;

                #region QueryAssets

                _QueryAssets.AppendLine("Insert into tb_Utility_Asset_XMissionStructures ");
                _QueryAssets.AppendLine(" (");
                _QueryAssets.AppendLine("     Utility_Asset_XMissionStructureId,");
                _QueryAssets.AppendLine("     UtilityFK,");
                _QueryAssets.AppendLine("     Latitude,");
                _QueryAssets.AppendLine("     Longitude,");
                _QueryAssets.AppendLine("     Altitude,");
                _QueryAssets.AppendLine("     SatelliteCount,");
                _QueryAssets.AppendLine("     SatelliteTime,");
                _QueryAssets.AppendLine("     SolutionAvailable,");
                _QueryAssets.AppendLine("     Handheld,");
                _QueryAssets.AppendLine("     XMissionStructures_TopTypeFk,");
                _QueryAssets.AppendLine("     Utility_ContactFK,");
                _QueryAssets.AppendLine("     Date,");
                _QueryAssets.AppendLine("     Utility_ProjectFK");
                _QueryAssets.AppendLine(" )Values(");
                _QueryAssets.AppendLine("     @Utility_Asset_XMissionStructureId,");
                _QueryAssets.AppendLine("     @UtilityFK,");
                _QueryAssets.AppendLine("     @Latitude,");
                _QueryAssets.AppendLine("     @Longitude,");
                _QueryAssets.AppendLine("     @Altitude,");
                _QueryAssets.AppendLine("     @SatelliteCount,");
                _QueryAssets.AppendLine("     @SatelliteTime,");
                _QueryAssets.AppendLine("     @SolutionAvailable,");
                _QueryAssets.AppendLine("     @Handheld,");
                _QueryAssets.AppendLine("     @XMissionStructures_TopTypeFk,");
                _QueryAssets.AppendLine("     @Utility_ContactFK,");
                _QueryAssets.AppendLine("     @Date,");
                _QueryAssets.AppendLine("     @Utility_ProjectFK");
                _QueryAssets.AppendLine(" )");
                _MobileAssets.Query = _QueryAssets.ToString();


                #endregion

                foreach (EUtility_Asset_XMissionStructure asset in listAssets)
                {
                    _ParametersAssets = new List<SqlParameter>();

                    #region ParametersAssets
                    _ParametersAssets.Add(DBHelper.CreateIntParameter("@Utility_Asset_XMissionStructureId", ParameterDirection.Input, false, asset.Id));
                    _ParametersAssets.Add(DBHelper.CreateIntParameter("@SolutionAvailable", ParameterDirection.Input, false, asset.SolutionAvailable));
                    _ParametersAssets.Add(DBHelper.CreateIntParameter("@Handheld", ParameterDirection.Input, false, DBNull.Value));

                    #region Utility
                    if (asset.Utility.Id != Int32.MinValue)
                        _ParametersAssets.Add(DBHelper.CreateIntParameter("@UtilityFK", ParameterDirection.Input, false, asset.Utility.Id));
                    else
                        _ParametersAssets.Add(DBHelper.CreateIntParameter("@UtilityFK", ParameterDirection.Input, true, DBNull.Value));
                    #endregion

                    #region Contact
                    if (asset.Contact.Id != Int32.MinValue)
                        _ParametersAssets.Add(DBHelper.CreateIntParameter("@Utility_ContactFK", ParameterDirection.Input, false, asset.Contact.Id));
                    else
                        _ParametersAssets.Add(DBHelper.CreateIntParameter("@Utility_ContactFK", ParameterDirection.Input, true, DBNull.Value));
                    #endregion

                    #region Project
                    if (asset.Project.Id != Int32.MinValue)
                        _ParametersAssets.Add(DBHelper.CreateIntParameter("@Utility_ProjectFK", ParameterDirection.Input, false, asset.Project.Id));
                    else
                        _ParametersAssets.Add(DBHelper.CreateIntParameter("@Utility_ProjectFK", ParameterDirection.Input, true, DBNull.Value));
                    #endregion

                    #region Toptype
                    if (asset.TopType.XMissionStructures_TopTypeId != Int32.MinValue)
                        _ParametersAssets.Add(DBHelper.CreateIntParameter("@XMissionStructures_TopTypeFk", ParameterDirection.Input, false, asset.TopType.XMissionStructures_TopTypeId));
                    else
                        _ParametersAssets.Add(DBHelper.CreateIntParameter("@XMissionStructures_TopTypeFk", ParameterDirection.Input, true, DBNull.Value));
                    #endregion

                    #region Latitude
                    if (asset.Latitude != Double.MinValue)
                        _ParametersAssets.Add(DBHelper.CreateFloatParameter("@Latitude", ParameterDirection.Input, false, asset.Latitude));
                    else
                        _ParametersAssets.Add(DBHelper.CreateFloatParameter("@Latitude", ParameterDirection.Input, true, DBNull.Value));
                    #endregion

                    #region Longitude
                    if (asset.Longitude != Double.MinValue)
                        _ParametersAssets.Add(DBHelper.CreateFloatParameter("@Longitude", ParameterDirection.Input, false, asset.Longitude));
                    else
                        _ParametersAssets.Add(DBHelper.CreateFloatParameter("@Longitude", ParameterDirection.Input, true, DBNull.Value));
                    #endregion

                    #region Altitude
                    if (asset.Altitude != Double.MinValue)
                        _ParametersAssets.Add(DBHelper.CreateFloatParameter("@Altitude", ParameterDirection.Input, false, asset.Altitude));
                    else
                        _ParametersAssets.Add(DBHelper.CreateFloatParameter("@Altitude", ParameterDirection.Input, true, DBNull.Value));
                    #endregion

                    #region SatelliteCount
                    if (asset.SatelliteCount != Int32.MinValue)
                        _ParametersAssets.Add(DBHelper.CreateIntParameter("@SatelliteCount", ParameterDirection.Input, false, asset.SatelliteCount));
                    else
                        _ParametersAssets.Add(DBHelper.CreateIntParameter("@SatelliteCount", ParameterDirection.Input, true, DBNull.Value));
                    #endregion

                    #region SatelliteTime
                    if (asset.SatelliteTime != DateTime.MinValue)
                        _ParametersAssets.Add(DBHelper.CreateDateTimeParameter("@SatelliteTime", ParameterDirection.Input, false, asset.SatelliteTime));
                    else
                        _ParametersAssets.Add(DBHelper.CreateDateTimeParameter("@SatelliteTime", ParameterDirection.Input, true, DBNull.Value));
                    #endregion

                    #region Date
                    if (asset.Date != DateTime.MinValue)
                        _ParametersAssets.Add(DBHelper.CreateDateTimeParameter("@Date", ParameterDirection.Input, false, asset.Date));
                    else
                        _ParametersAssets.Add(DBHelper.CreateDateTimeParameter("@Date", ParameterDirection.Input, true, DBNull.Value));
                    #endregion

                    #endregion

                    _MobileAssets.Parameters.Add(_ParametersAssets);
                }
                listQueries.Add(_MobileAssets);
            }
            return listQueries;
        }

        private List<EUtility_Asset_XMissionStructure> ParseDatatableAsset(DataTable dtAssets)
        {
            List<EUtility_Asset_XMissionStructure> listAssets = new List<EUtility_Asset_XMissionStructure>();

            foreach (DataRow dr in dtAssets.Rows)
            {
                EUtility_Asset_XMissionStructure tmpAsset = new EUtility_Asset_XMissionStructure();
                tmpAsset.Contact = new EUtility_Contact(ConvertHelper.ToInt32(dr["Utility_ContactFK"]));
                tmpAsset.Date = ConvertHelper.ToDateTime(dr["Date"]);
                tmpAsset.Handheld = new EHandheld(ConvertHelper.ToInt32(dr["Handheld"]),"");
                tmpAsset.Id = ConvertHelper.ToInt32(dr["Utility_Asset_XMissionStructureId"]);
                tmpAsset.ServerID = tmpAsset.Id;
                tmpAsset.Latitude = ConvertHelper.Todouble(dr["Latitude"]);
                tmpAsset.Longitude = ConvertHelper.Todouble(dr["Longitude"]);
                tmpAsset.Altitude = ConvertHelper.Todouble(dr["Altitude"]);
                tmpAsset.Project = new EUtility_Project(ConvertHelper.ToInt32(dr["Utility_ProjectFK"]));
                tmpAsset.SatelliteCount = ConvertHelper.ToInt32(dr["SatelliteCount"]);
                tmpAsset.SatelliteTime = ConvertHelper.ToDateTime(dr["SatelliteTime"]);
                tmpAsset.TopType = new EXMissionStructures_TopType(ConvertHelper.ToInt32(dr["XMissionStructures_TopTypeFk"]));
                tmpAsset.Utility = new EUtility(ConvertHelper.ToInt32(dr["UtilityFK"]));
                listAssets.Add(tmpAsset);
            }
            return listAssets;
        }

        public static DataSet SelectAllNearByLatitudeLongitude(double latitude, double longitude, string connectionKey)
        {
            return DUtility_Asset_XMissionStructure.SelectAllNearByLatitudeLongitude(latitude, longitude, connectionKey);
        }
    }
}
